package com.bonc.excel.sql;

import cn.hutool.core.date.DateUtil;
import com.mysql.cj.MysqlConnection;
import com.mysql.cj.Query;
import com.mysql.cj.exceptions.ExceptionFactory;
import com.mysql.cj.interceptors.QueryInterceptor;
import com.mysql.cj.jdbc.JdbcConnection;
import com.mysql.cj.log.Log;
import com.mysql.cj.protocol.Resultset;
import com.mysql.cj.protocol.ServerSession;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.function.Supplier;


/**
 * @Author: wujing
 * @Date: 2024/10/9 16:31
 * @Description: TODO
 **/

//@Component
public class Mysql8Query2Interceptor implements QueryInterceptor {
    private Map<String, String> preExecuteValues = new HashMap<>();

    private Map<String, String> postExecuteValues = new HashMap<>();

    private JdbcConnection connection;

    private Log log;

    @Override
    public QueryInterceptor init(MysqlConnection conn, Properties props, Log l) {
        this.connection = (JdbcConnection) conn;
        this.log = l;
        return this;
    }

    @Override
    public <T extends Resultset> T postProcess(Supplier<String> sql, Query interceptedQuery, T originalResultSet, ServerSession serverSession) {
        System.out.println("拦截器2 postProcess");
       // System.out.println(DateUtil.format(new Date(),"yyyyMMddHHmmssSSS"));
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return null; // we don't actually modify a result set

    }


    @Override
    public <T extends Resultset> T preProcess(Supplier<String> sql, Query interceptedQuery) {

        System.out.println("拦截器2 preProcess");
        //System.out.println(DateUtil.format(new Date(),"yyyyMMddHHmmssSSS"));
        return null; // we don't actually modify a result set
    }

    @Override
    public boolean executeTopLevelOnly() {
        return true;
    }

    @Override
    public void destroy() {
        this.connection = null;
        this.log = null;
    }
}
